package
{
import com.vcommon.core.logging.Tracer;

import flash.display.Bitmap;

import fr.kouma.starling.utils.Stats;

import org.josht.starling.display.Sprite;
import org.josht.starling.foxhole.controls.Button;

import starling.core.Starling;
import starling.display.Image;
import starling.display.MovieClip;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
import starling.textures.Texture;
import starling.textures.TextureAtlas;

/**
 * author yangwz
 */		
public class Game extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>Game</code> instance.
	 * 
	 */
	public function Game()
	{
		super();
		addEventListener(Event.ADDED_TO_STAGE, initDisplay);
	}
	
	private var mc:MovieClip;
	
	[Embed(source="assets/Lover.xml", mimeType="application/octet-stream")]
	private static const LoverSheetXml:Class;
	
	[Embed(source="assets/Lover.png")]
	private static const Lover:Class;
	
	private var mcArr:Array = [];
	
	private var button:Button;
	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay(event:Event = null):void
	{
		var bitmap:Bitmap = new Lover() as Bitmap;
		var texture:Texture = Texture.fromBitmap(bitmap);
//		var xml:XML = new XML(new LoverSheetXml());
//		var textureAtlas:TextureAtlas = new TextureAtlas(texture, xml);
//		var frames:Vector.<Texture> = textureAtlas.getTextures("a");
//		for (var i:int = 0; i < 20; i++)
//		{
//			for (var j:int = 0; j < 8; j++)
//			{
//				mc = new MovieClip(frames, 20);
//				mc.x = mc.width * i;
//				mc.y = mc.height * j + 100;
//				addChild(mc);
//				mc.stop();
//				mcArr.push(mc);
//				Starling.juggler.add(mc);
//			}
//		}
//		stage.addEventListener(TouchEvent.TOUCH, onTouchHandler);
//		addChild(new Stats());
		var image:Image = new Image(texture);
		
		button = new Button();
		button.defaultSkin = image;
		addChild(button);
		button.addEventListener(TouchEvent.TOUCH, onTouchHandler);
	}
	
	private function onTouchHandler(event:TouchEvent):void
	{
		var touch:Touch = event.getTouch(stage);
		if (touch.phase == TouchPhase.ENDED)
		{
//			for (var i:int = 0; i < mcArr.length; i++)
//			{
//				var mc:MovieClip = mcArr[i];
//				mc.play();
//			}
			trace("ok");
		}
		else if (touch.phase == TouchPhase.MOVED)
		{
			button.x = touch.globalX;
			button.y = touch.globalY;
		}
	}
	
	private function mcPlayCompleteHandler(event:Event):void
	{
		mc.stop();
	}
}
}